package cn.jsj.hzu.qm.facecloud.user.servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jsj.hzu.qm.facecloud.pyAi.LACConnecter;
import cn.jsj.hzu.qm.facecloud.pyAi.LACObject;
import cn.jsj.hzu.qm.facecloud.utils.FileProcesser;

/**
 * Servlet implementation class JpgUploadFaceTestServlet
 */
public class JpgUploadFaceTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public JpgUploadFaceTestServlet() {
		super();
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		System.out.println("");
	}
	

	protected String testFace(List<Integer> list) {
	
//		FileOutputStream out = new FileOutputStream(new File("D:\\out.txt"));
		
		/**
		 * 用于返回的字符串
		 * */
		String rString = "NO CONTEXT";
		
		//1 新建对象
		LACConnecter lacConnecter = new LACConnecter();
		
		//2 获取链接 
		LACObject lacObject = lacConnecter.getConnectionLacObject();
		if (lacObject == null) {
			return ("服务器忙<br>server is busy");
			
		}
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//3 设置文本
		lacObject.setInJpgImg(list);
		
		//4 启动
		int i = lacObject.setStateToRun();
		if (i == -1) {
//			out.write("Error in start".getBytes());
			return "Error in start";
		}
		
		//5 等待并获取结果
		
		long sleep = 0;
		boolean isGood = false;
		while (sleep <= 5000) {
			if (lacObject.getState().equals(LACObject.STATE_OUT_READY) == true) {
				rString = lacObject.getOutString();
				isGood = true;
				break;
			}
			
			try {
				sleep += 300;
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(isGood == false) {
//			out.write("OVER TIME-服务器内部响应超时".getBytes());
			return "OVER TIME-服务器内部响应超时";
		}
		
		//6 释放
		lacConnecter.free(lacObject);
		return rString;
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
//		StudentService studentService = BeanFactory.getBean("studentService");
		
		FileProcesser fileProcesser = new FileProcesser(request.getInputStream());
	
		LinkedList<LinkedList<Integer>> list = fileProcesser.getSplitBySymble();
		/*
//JS:		
//		formData = new FormData();
//		formData.append("photo", $myfile[0].files[0]);
//		formData.append("message", '<%=stuId%>');
//		formData.append("A", 'A');
//		formData.append("B", 'B');
		*/
		
		//参数数量不匹配
		if (list.size() != (4*2)+1) {
			response.getWriter().write("Count error "+list.size()+"(9)");
			return;
		}
		
		
		
		
		
		
		//获取id
		LinkedList<Integer> stuIdLinkedList = list.get(3);
		StringBuilder stuIdBuilder = new StringBuilder();
		//注意，需要去掉末尾4个标识符
		for(int i = 0; i < stuIdLinkedList.size()-4; i++) {
			stuIdBuilder.append((char)((int)stuIdLinkedList.get(i)));
		}
		System.out.println(stuIdLinkedList);
		String stuIdString = stuIdBuilder.toString();
		System.out.println(stuIdString);
		int stuId = Integer.parseInt(stuIdString);
		
		//获取图片
		LinkedList<Integer> photoLinkedList = list.get(1);
		
		String rString = testFace(photoLinkedList);
		response.getWriter().write(rString);
		return;
		
	}

}
